import { _decorator, Component, director, find, game, Game, instantiate, Label, Node, Sprite, UITransform, Widget } from 'cc';
import { EconomicCom } from '../../Entity/Component/EconomicCom';
import { Global, TechnologyType } from '../../Util/Global';
import GameEventMgr, { GameEvent } from '../../Model/GameEventMgr';
import { DestructableCom } from '../../Entity/Component/DestructableCom';
const { ccclass, property } = _decorator;

@ccclass('EconIconUI')
export class EconIconUI extends Component {

    @property(Node)
    shield_icon: Node = null;
    @property(Node)
    shield_content: Node = null;
    @property(Node)
    power_icon: Node = null;
    @property(Node)
    power_content: Node = null;
    @property(Node)
    metal_icon: Node = null;
    @property(Node)
    metal_content: Node = null;

    econCom: EconomicCom = null;

    start() {

        GameEventMgr.Register(this, GameEvent.GameStart, this.RefreshUISize.bind(this));
        GameEventMgr.Register(this, GameEvent.PlayerDataChange, this.RefreshUISize.bind(this));
        game.on(Game.EVENT_SHOW, this.RefreshUISize.bind(this));
        // 监听窗口的 resize 事件
        window.addEventListener('resize', this.RefreshUISize.bind(this));
    }

    protected onDestroy(): void {
        game.off(Game.EVENT_SHOW, this.RefreshUISize.bind(this));
        // 监听窗口的 resize 事件
        window.removeEventListener('resize', this.RefreshUISize.bind(this));
    }


    RefreshPowerContent() {
        if (this.econCom == null) {
            return;
        }
        let need_count = Global.gameManager.playerData.level || 1;
        let power_max = this.econCom.electric_power_max || 100;
        let power_remain = this.econCom.electric_power_remain;
        let one_num = power_max / need_count;

        let full_count = Math.floor(this.econCom.power_out / one_num);
        let charge_ratio = (this.econCom.power_out % one_num) / one_num;
        let cells = this.power_content.children;
        for (let i = 0; i < need_count; i++) {
            let cell = cells[i];
            if (cell) {
                cell.active = true;
            }
            else {
                cell = instantiate(cells[0]);
                cell.parent = this.power_content;
                cell.active = true;
            }

            if (i < full_count) {
                find('content_full', cell).getComponent(Sprite).fillRange = 1;
            }
            else if (i == full_count) {
                find('content_full', cell).getComponent(Sprite).fillRange = charge_ratio;
            }
            else {
                find('content_full', cell).getComponent(Sprite).fillRange = 0;
            }
        }
        for (let i = 1; i < cells.length; i++) {
            if (i >= need_count) {
                cells[i].destroy();
            }
        }

        find('num', this.power_icon).getComponent(Label).string = (this.econCom.power_load * 100).toFixed(1) + '%';
        find('io_ratio', this.node).getComponent(Sprite).fillStart = 1 - power_remain / power_max;
        find('io_ratio/num', this.node).getComponent(Label).string = power_remain.toFixed(1);
    }

    RefreshMetalContent() {
        if (this.econCom == null) {
            return;
        }
        let metal_max = Global.gameManager.playerData.level * 200;
        let cell = this.metal_content.children[0];
        find('content_full', cell).getComponent(Sprite).fillRange = this.econCom.mine_res / metal_max;
        find('num', this.metal_icon).getComponent(Label).string = this.econCom.mine_res.toFixed(1);

    }

    RefreshShieldContent() {
        if (this.shield_icon.active == false) {
            return;
        }
        let player = Global.gameManager.playerUnit;
        if (player && player.isValid) {
            let destructable = player.getComponent(DestructableCom);
            if (destructable) {
                find('num', this.shield_icon).getComponent(Label).string = destructable.shield.toFixed(1);
                let cell = this.shield_content.children[0];
                find('content_full', cell).getComponent(Sprite).fillRange = destructable.shield / destructable.maxShield;
            }
        }

    }

    RefreshUISize() {
        let playerdata = Global.gameManager.playerData;

        let shield = playerdata.technologyProduct.get(TechnologyType.Shield) > 0;
        this.shield_icon.active = shield;
        this.shield_content.active = shield;

        this.scheduleOnce(() => {
            let width = this.node.getChildByName('content').getComponent(UITransform).width;
            this.getComponent(UITransform).width = width;

            find('io_ratio', this.node).getComponent(UITransform).width = width - 25 - (width - 220) / 8;
        });
    }
}

